var express = require('express');
var router = express.Router();
var jwtAuth = require("../jwtAuth")
var db = require("ykt-mongo")


router.use(jwtAuth)

// 添加新闻
router.post("/", function (req, res, next) {
    console.log("添加新闻");
    let newsData = req.body
    db.collection("news").insert(newsData, function (data) {
        if (data.result.ok == 1 && data.result.n == 1) {
            console.log("添加新闻成功！");
            res.send({
                code: 0,
                msg: "添加成功！"
            })
        } else {
            res.send({
                code: 1,
                msg: "添加失败！"
            })
        }
    })
})

//---------------------------------
//渲染页面
router.get('/',function(req,res,next){
    let {page,limit}=req.query;
    // console.log(111,page,limit);
    db.collection("news").findByPage(page,limit,{},function(data){
        console.log("===============",data);
        res.send({
            code:0,
            data
        })
    })
})
//---------------------------------


/* GET home page. */
router.get('/list', function (req, res, next) {
    res.send({
        code: 0,
        list: []
    })
});

module.exports = router;
